var layer;
var form;
var table;
var lay;

layui.use(['layer', 'form', 'table'], function () {
    layer = layui.layer;
    form = layui.form;
    table = layui.table;

    // 执行一个 table 实例=============================================================================================
    table.render({
        elem: '#data_table',
        id: 'data_table',
        url: ctxPath + 'generator/select',
        title: '版本表',
        //height: 'full-120',
        //size:"sm",
        page: true,
        toolbar: "#toolberLeft",
        cols: [[
            {field: 'projectPath', title: '代码生成路径', minWidth: '200', hide: setColumnHide("projectPath")},
            {field: 'comPath', title: '追加路径', minWidth: '200', hide: setColumnHide("comPath")},
            {field: 'author', title: '作者', minWidth: '200', hide: setColumnHide("author")},
            {field: 'openState', title: '生成后是否打开目录', minWidth: '200', hide: setColumnHide("openState"),
                templet: function (d) {
                    if(d.openState == 1){
                        return '是';
                    }
                    return '否';
                }
            },
            {field: 'swagger2', title: '实体类添加swagger2注解', minWidth: '200', hide: setColumnHide("swagger2"),
                templet: function (d) {
                    if(d.swagger2 == 1){
                        return '是';
                    }
                    return '否';
                }
            },
            {field : 'state', title : '状态', align : 'center', minWidth: '80', hide: setColumnHide("state"), toolbar : '#switchRow'},
            {field: 'beginTableName', minWidth: '150', title: '表前缀', hide: setColumnHide("beginTableName")},
            {fixed: isRight(), align: 'center', minWidth: '250', title: '操作', toolbar: '#toolRow'}
        ]]
        , done: function (result, currPage, count) {
            form.render('checkbox');
        }
    });

    // 头部左侧事件=============================================================================================
    table.on('toolbar(data_table)', function (obj) {
        if (obj.event === 'add') {
            openWindowRB('data_form', "新增", '700px', '100%', false, 0, false, false, null, 0.2, false);
        }
    })

    // 行监听事件=============================================================================================
    table.on('tool(data_table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            deleteRow(table, 'data_table', data, ctxPath + 'generator/delete' + "?id=" + data.id);
        } else if (obj.event === 'edit') {
            openWindowRB('data_form', "编辑", '700px', '100%', false, 0, false, false, data, 0.2, false);
        } else if (obj.event === 'detail') {
            openWindowRB('data_form', "查看", '700px', '100%', false, 0, false, false, data, 0.2, false);
        }
    });


    // 提交=============================================================================================
    form.on('submit(submitForm)', function (data) {
        data.field['notField'] = replaceAll(data.field['notField'], "，", ",");
        saveData(table, 'data_table', data.field, ctxPath + 'generator/save', '#data_form');
        return false;
    })

    // 开关=============================================================================================
    form.on('switch(stateSwitch)', function(data) {
        var id = $(this).attr('yid');
        var state = data.elem.checked?"1":"0";
        getAjax(ctxPath + 'generator/updateState', {id: id, state: state}, 'post', function (res) {
            showTips(res.msg);
            reloadTable(table, 'data_table');
        })
        return false;
    })



})


